package com.zmn.oms.third.tc58;

import com.alibaba.fastjson.JSON;
import com.zmn.oms.common.utils.HttpUtil;
import com.zmn.oms.third.base.dto.api.ApiInvokResult;
import com.zmn.oms.third.core.tracker.FacadeTracker;
import com.zmn.oms.third.tc58.dto.Tc58Request;
import com.zmn.oms.third.tc58.dto.api.Tc58Order;
import com.zmn.oms.third.tc58.dto.api.Tc58PayLink;
import com.zmn.oms.third.tc58.util.Tc58Util;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/04/23 11:07
 */
@Slf4j
public final class Tc58ApiInvoker {
    private static final String LOG_HEAD = "58同城-API";


    /**
     * 调用api
     * @param path
     * @param data
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> ApiInvokResult<T> apiInvok(String path, Map<String, Object> data, Class<T> tClass) {
        String url = Tc58Consts.API_URL + path;

        Tc58Request<Map<String, Object>> params = new Tc58Request<>();
        params.setData(data);
        params.setSign(Tc58Util.sign(Tc58Consts.SECRET, data, null));
        String paramsStr = JSON.toJSONString(params);

        FacadeTracker.FacadeTrackerCore tracker = FacadeTracker.getTracker();
        ApiInvokResult<T> result = null;
        String response = null;
        try {
            log.info("【{}】<Request-tc58> Url:[{}] RequestBody:[{}]", LOG_HEAD, url, paramsStr);
            tracker.reqSummary(paramsStr);

            response = HttpUtil.httpsPost(url, paramsStr);

            tracker.respSummary(response);
            log.info("【{}】<Response-tc58> ResponseBody:[{}]", LOG_HEAD, response);

            result = Tc58Util.parseResponse(response, tClass);
        } catch (Exception e) {
            result = new ApiInvokResult<>();
            result.setSuccess(false);
            result.setMsg(e.getMessage());

            tracker.error(e, null);

            log.info("【{}】 请求失败:[{}]", LOG_HEAD, e.getMessage());
            log.error(e.getMessage(), e);
        }

        return result;
    }

    /**
     * 查询订单信息
     * @param outerId
     * @return
     */
    public static Tc58Order queryOrderInfo(String outerId, boolean isJingxuan, String appkey) {
        String currAppkey = isJingxuan ? appkey : Tc58Consts.APPKEY;
        Map<String, Object> data = new HashMap<>();
        data.put("clientId", currAppkey);
        data.put("orderId", outerId);

        String path = isJingxuan ? Tc58Consts.PATH_QUERY_ORDER_JX : Tc58Consts.PATH_QUERY_ORDER;
        ApiInvokResult<Tc58Order> result = apiInvok(path, data, Tc58Order.class);
        log.info("【{}】查询订单：OuterId:[{}]，Result:[{}]", LOG_HEAD, outerId, JSON.toJSONString(result));

        return result.getData();
    }

    /**
     * 查询订单信息
     * @param outerId
     * @return
     */
    public static ApiInvokResult payLink(String outerId, Double orderMoney, boolean isJingxuan) {
        String appkey = isJingxuan ? Tc58Consts.APPKEY_JX : Tc58Consts.APPKEY;
        Map<String, Object> data = new HashMap<>();
        data.put("clientId", appkey);
        data.put("orderId", outerId);
        data.put("orderMoney", String.format("%.2f", orderMoney));

        ApiInvokResult<Tc58PayLink> result = apiInvok(Tc58Consts.PATH_PAY_LINK, data, Tc58PayLink.class);
        log.info("【{}】查询订单：OuterId:[{}]，Result:[{}]", LOG_HEAD, outerId, JSON.toJSONString(result));

        return result;
    }

    /**
     * 私有构造器，禁止外部实例化
     */
    private Tc58ApiInvoker() {}


    public static void main(String[] args) {
        Tc58Consts.API_URL = "https://bsteward.58.com";
        Tc58Consts.SECRET = "fjieo291j2k19dkao291jfle0218dke2";

        Map<String, Object> data = new HashMap<>();
        data.put("clientId", "39602242796558");
        data.put("orderId", "3412217143074488331");
        data.put("flag", 1);    // flag:1接单、2不接单、3取消单
        /*
        refuseReason:拒单时传
            0：非拒单时传这个值
            1：疫情期间，小区不允许外部人员进入
            2：疫情期间，无法提供服务
            3：这个时间段已经约满
            4：暂时无法提供这个服务
            5：暂时不支持这个小区的服务
            6：联系不到您
         */
        data.put("refuseReason", 0);

        //ApiInvokResult result = Tc58ApiInvoker.apiInvok(Tc58Consts.PATH_ACCEPT_ORDER_JX, data, Boolean.class);
        Tc58Order tc58Order = Tc58ApiInvoker.queryOrderInfo("3412217143074488331", true, "39602242796558");
        System.out.println(tc58Order.getPhone());
    }
}
